package com.bonc.ioc.controller;

import com.bonc.ioc.base.controller.BaseController;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.common.util.AppReply;
import com.bonc.ioc.model.Opinion;
import com.bonc.ioc.service.OpinionService;
import com.bonc.ioc.validation.AppAuthAnnotation;
import com.bonc.ioc.vo.OpinionVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;

/**
 * @author wxf
 * @Title
 * @Package
 * @Description 意见处理
 * @date 2021/4/711:23
 */
@RestController
@RequestMapping("/opinion")
public class OpinionController extends BaseController {

    @Resource
    private OpinionService opinionService;

    @PostMapping("/insert")
    @AppAuthAnnotation
    public AppReply insertOpinion(@RequestBody @NotBlank OpinionVo opinionVo){
        String title = opinionVo.getTitle().trim();
        String opinionInfo = opinionVo.getOpinionInfo().trim();
        if (StringUtils.isEmpty(title) || StringUtils.isEmpty(opinionInfo)){
            return AppReply.error("请填写完整意见信息");
        }
        opinionService.insertOpinion(title,opinionInfo);
        return AppReply.success("插入成功");
    }

    @RequestMapping("/selectByPage")
    @AppAuthAnnotation
    public AppReply selectOpinionByPage(@RequestParam String pageSize,
                                        @RequestParam String pageNum,
                                        @RequestParam String title,
                                        @RequestParam String state) throws BindException {
        HashMap<String,Object> map = new HashMap<>(16);
        title = title.trim();
        map.put("title",title);
        map.put("state",state);
        PageResult pageResult = opinionService.selectOpinionByPage(map);
        return AppReply.success(pageResult);
    }

    @RequestMapping("/handle")
    @AppAuthAnnotation
    public AppReply handleOpinion(@RequestParam @NotBlank String id){
        opinionService.handleOpinion(id);
        return AppReply.success("处理成功");
    }

    @RequestMapping("/selectById")
    @AppAuthAnnotation
    public AppReply selectById(@RequestParam @NotBlank String id) {
        String trimId = id.trim();
        if (StringUtils.isEmpty(trimId)){
            return AppReply.error("意见ID未传值");
        }
        Opinion opinion = opinionService.selectById(id);
        return AppReply.success(opinion);
    }
}
